﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SZSLSystem.Common;
using SZSLSystem.DBUtility;
using SZSLSystem.Model;

namespace SZSLSystem.DAL
{
    /// <summary>
    /// 数据访问层 客户
    /// </summary>
    public partial class Customer
    {
        public Customer()
        {

        }

        #region  Method

        /// <summary>
        ///  判断是否存在
        /// </summary>
        /// <param name="Id">管理员编号</param>
        /// <returns></returns>
        public bool Exists(Guid Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Customer");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.UniqueIdentifier)};
            parameters[0].Value = Id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        ///  判断客户姓名是否存在
        /// </summary>
        /// <param name="UserName">客户姓名</param>
        /// <returns></returns>
        public bool Exists(string CustomerName)
        {
            string strSql = "select count(*) from Customer where CustomerName=@CustomerName";
            SqlParameter[] parameters = {
                new SqlParameter("@CustomerName",SqlDbType.NVarChar,30)};
            parameters[0].Value = CustomerName;
            return DbHelperSQL.Exists(strSql, parameters);
        }

        /// <summary>
        /// 返回数据总数
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        /// <returns></returns>
        public int GetCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(*) as H ");
            strSql.Append(" from Customer ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return Convert.ToInt32(DbHelperSQL.GetSingle(strSql.ToString()));
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        /// <param name="model">管理员信息实体</param>
        /// <returns></returns>
        public int Add(SZSLSystem.Model.Customer model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Customer(");
            strSql.Append("[ID],[CustomerName],[Sex],[IDCard],[BrithDay],[Mobile],[Telphone],[Address],[Sort],[AddDate],[UserID])");
            strSql.Append("values (@ID,@CustomerName,@Sex,@IDCard,@BrithDay,@Mobile,@Telphone,@Address,@Sort,@AddDate,@UserID)");
            SqlParameter[] parameters = {
					new SqlParameter ("@ID",SqlDbType.UniqueIdentifier),
					new SqlParameter ("@CustomerName",SqlDbType.UniqueIdentifier),
					new SqlParameter ("@Sex",SqlDbType.Int,4),
					new SqlParameter ("@IDCard",SqlDbType.VarChar,50),
                    new SqlParameter ("@BrithDay",SqlDbType.DateTime),
					new SqlParameter ("@Mobile",SqlDbType.VarChar,50),
					new SqlParameter ("@Telphone",SqlDbType.VarChar,50),
					new SqlParameter ("@Address",SqlDbType.VarChar,500),
					new SqlParameter ("@Sort",SqlDbType.Int,4),
					new SqlParameter ("@AddDate",SqlDbType.DateTime),
					new SqlParameter ("@UserID",SqlDbType.UniqueIdentifier)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.CustomerName;
            parameters[2].Value = model.Sex;
            parameters[3].Value = model.IDCard;
            parameters[4].Value = model.BrithDay;
            parameters[5].Value = model.Mobile;
            parameters[6].Value = model.Telphone;
            parameters[7].Value = model.Address;
            parameters[8].Value = model.Sort;
            parameters[9].Value = model.AddDate;
            parameters[10].Value = model.UserID;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新客户
        /// </summary>
        /// <param name="model">客户信息实体</param>
        /// <returns></returns>
        public bool Update(SZSLSystem.Model.Customer model)
        {
            SZSLSystem.Model.Customer curModel = GetModel(model.CustomerName);
            if (curModel == null)
            {
                return false;
            }

            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Customer set ");
            strSql.Append("[CustomerName]=@CustomerName,");
            strSql.Append("[Sex]=@Sex,");
            strSql.Append("[IDCard]=@IDCard,");
            strSql.Append("[BrithDay]=@BrithDay,");
            strSql.Append("[Mobile]=@Mobile,");
            strSql.Append("[Telphone]=@Telphone,");
            strSql.Append("[Address]=@Address,");
            strSql.Append("[Sort]=@Sort,");
            strSql.Append("[AddDate]=@AddDate,");
            strSql.Append("[UserID]=@UserID ");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter ("@ID",SqlDbType.UniqueIdentifier),
					new SqlParameter ("@CustomerName",SqlDbType.UniqueIdentifier),
					new SqlParameter ("@Sex",SqlDbType.Int,4),
					new SqlParameter ("@IDCard",SqlDbType.VarChar,50),
                    new SqlParameter ("@BrithDay",SqlDbType.DateTime),
					new SqlParameter ("@Mobile",SqlDbType.VarChar,50),
					new SqlParameter ("@Telphone",SqlDbType.VarChar,50),
					new SqlParameter ("@Address",SqlDbType.VarChar,500),
					new SqlParameter ("@Sort",SqlDbType.Int,4),
					new SqlParameter ("@AddDate",SqlDbType.DateTime),
					new SqlParameter ("@UserID",SqlDbType.UniqueIdentifier)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.CustomerName;
            parameters[2].Value = model.Sex;
            parameters[3].Value = model.IDCard;
            parameters[4].Value = model.BrithDay;
            parameters[5].Value = model.Mobile;
            parameters[6].Value = model.Telphone;
            parameters[7].Value = model.Address;
            parameters[8].Value = model.Sort;
            parameters[9].Value = model.AddDate;
            parameters[10].Value = model.UserID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除客户
        /// </summary>
        /// <param name="Id">客户编号</param>
        /// <returns></returns>
        public bool Delete(Guid Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Customer ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.UniqueIdentifier)
            };
            parameters[0].Value = Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 根据客户姓名获取数据
        /// </summary>
        /// <param name="userName">客户姓名</param>
        /// <returns></returns>
        public SZSLSystem.Model.Customer GetModel(string CustomerName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 * from Customer");
            strSql.Append(" where CustomerName=@CustomerName ");
            SqlParameter[] parameters = {
					new SqlParameter("@CustomerName", SqlDbType.VarChar,50)};
            parameters[0].Value = CustomerName;

            SZSLSystem.Model.Customer model = new SZSLSystem.Model.Customer();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow row = ds.Tables[0].Rows[0];
                if (row["ID"].ToString() != "")
                {
                    model.ID = (Guid)row["ID"];
                }
                model.CustomerName = row["CustomerName"].ToString();
                if (row["Sex"].ToString() != "")
                {
                    model.Sex = int.Parse(row["Sex"].ToString());
                }
                model.IDCard = row["IDCard"].ToString();
                model.BrithDay = DateTime.Parse(row["BrithDay"].ToString());
                model.Mobile = row["Mobile"].ToString();
                model.Telphone = row["Telphone"].ToString();
                model.Address = row["Address"].ToString();
                model.Sort = int.Parse(row["Sort"].ToString());
                model.AddDate = DateTime.Parse(row["AddDate"].ToString());
                model.UserID = Guid.Parse(row["UserID"].ToString());

                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 根据客户编号获取数据
        /// </summary>
        /// <param name="Id">客户编号</param>
        /// <returns></returns>
        public SZSLSystem.Model.Customer GetModel(Guid Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 * from Customer ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.UniqueIdentifier)
            };
            parameters[0].Value = Id;

            SZSLSystem.Model.Customer model = new SZSLSystem.Model.Customer();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow row = ds.Tables[0].Rows[0];
                if (row["ID"].ToString() != "")
                {
                    model.ID = (Guid)row["ID"];
                }
                model.CustomerName = row["CustomerName"].ToString();
                if (row["Sex"].ToString() != "")
                {
                    model.Sex = int.Parse(row["Sex"].ToString());
                }
                model.IDCard = row["IDCard"].ToString();
                model.BrithDay = DateTime.Parse(row["BrithDay"].ToString());
                model.Mobile = row["Mobile"].ToString();
                model.Telphone = row["Telphone"].ToString();
                model.Address = row["Address"].ToString();
                model.Sort = int.Parse(row["Sort"].ToString());
                model.AddDate = DateTime.Parse(row["AddDate"].ToString());
                model.UserID = Guid.Parse(row["UserID"].ToString());

                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得客户数据列表
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        /// <returns></returns>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" from Customer ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by AddDate desc");
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得客户前几行数据
        /// </summary>
        /// <param name="Top">行数</param>
        /// <param name="strWhere">查询条件</param>
        /// <param name="filedOrder">排序条件</param>
        /// <returns></returns>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" * ");
            strSql.Append(" from Customer ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得管客户查询分页数据
        /// </summary>
        /// <param name="pageSize">每页数目</param>
        /// <param name="currentPage">当前页数</param>
        /// <param name="strWhere">查询条件</param>
        /// <param name="filedOrder">排序条件</param>
        /// <returns></returns>
        public DataSet GetPageList(int pageSize, int currentPage, string strWhere, string filedOrder)
        {
            int topSize = pageSize * currentPage;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top " + pageSize + " * from Customer");
            strSql.Append(" where Id not in(select top " + topSize + " Id from Customer");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder + ")");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" and " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);

            return DbHelperSQL.Query(strSql.ToString());
        }

        #endregion  Method
    }
}
